from django.db import models

# Create your models here.
class DepotFile(models.Model):
    """
    A file which represents one source file in perforce depot.

    # Create some files
    >>> poiwz_c_rel = DepotFile.objects.create(remoteFile = "//BUSMB_B1/SBO/8.8_REL/Application/ObjMgr/POIWZ.cpp")
    >>> poiwz_c_dev = DepotFile.objects.create(remoteFile = "//BUSMB_B1/SBO/dev/Application/ObjMgr/POIWZ.cpp")

    # check file name
    >>> poiwz_c_rel.fileName()
    'POIWZ.cpp'
    >>> poiwz_c_dev.fileName()
    'POIWZ.cpp'
    """
    remoteFile = models.CharField(max_length = 200)

    def fileName(self):
        import string
        l = string.split(self.remoteFile, '/')
        if l:
            return l[-1]
        else:
            return None

class ChangeList (models.Model):
    """
    A change list which represents a modification in perforce repository.

    # Create some change lists
    >>> cl_1 = ChangeList.objects.create(change_id = "1", user = "i028161", description = "Test change list 1")
    >>> cl_2 = ChangeList.objects.create(change_id = "2", user = "i028160", description = "Test change list 2")
    >>> cl_3 = ChangeList.objects.create(change_id = "3", user = "i028161", description = "Test change list 3")

    # Query change lists
    >>> len(ChangeList.objects.all())
    3
    >>> len(ChangeList.objects.filter(user="i028161"))
    2
    """
    change_id = models.IntegerField(primary_key = True)
    user = models.CharField(max_length = 10)
    description = models.CharField(max_length = 200)

    def byUser(self, user = None):
        if user == None:
            return None
        else:
            changes = ChangeList.objects.get(user='i028161')
            return changes

class ChangeListFiles(models.Model):
    """

    """
    change_id = models.ForeignKey('ChangeList')
    remoteFile = models.ForeignKey('DepotFile')

